package com.zhou.listener;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.zhou.entity.vo.Answers;
import com.zhou.entity.vo.Questions;
import com.zhou.entity.vo.QuestionsAnswers;
import com.zhou.service.QuestionsAnswersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class QuestionsAnswersDataListener extends AnalysisEventListener<QuestionsAnswers> {

    private final QuestionsAnswersService questionsAnswersService;

    public QuestionsAnswersDataListener(QuestionsAnswersService questionsAnswersService) {
        this.questionsAnswersService = questionsAnswersService;
    }

    /**
     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */

    private static final int BATCH_COUNT = 5;
    List<QuestionsAnswers> list = new ArrayList<QuestionsAnswers>();

    @Override
    public void invoke(QuestionsAnswers data, AnalysisContext context) {
        log.info("解析到一条数据:{}", JSON.toJSONString(data));
        list.add(data);
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        log.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        log.info("{}条数据，开始存储数据库！", list.size());
        List<Questions> questions = new ArrayList<>();   //创建需要保存的实体类User集合
        List<Answers> answers = new ArrayList<>();   //创建需要保存的实体类Phone集合
        for (int i = 0; i < list.size(); i++) {
            try {
                Questions questions1 = new Questions();
                questions1.setQu_content(list.get(i).getQu_content()); //提取需要保存的用户的属性
                questions1.setCreate_time(list.get(i).getCreate_time());
                questions1.setCreate_person(list.get(i).getCreate_person());
                questions1.setQu_type(list.get(i).getQu_type());
                questions1.setLevel(list.get(i).getLevel());
                questions1.setImage(list.get(i).getImage());
                questions1.setQu_bank_id(list.get(i).getQu_bank_id());
                questions1.setQu_bank_name(list.get(i).getQu_bank_name());
                questions1.setAnalysis(list.get(i).getAnalysis());
                questions.add(questions1);                    //添加到集合
                Answers answers1 = new Answers();
                answers1.setAll_option(list.get(i).getAll_option());
                answers1.setImages(list.get(i).getImages());
                answers1.setQuestion_id(list.get(i).getQuestion_id());
                answers1.setTrue_option(list.get(i).getTrue_option());

                answers.add(answers1);                  //添加到集合
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (!CollectionUtils.isEmpty(questions)) {
            questionsAnswersService.saveUsers(questions);   //集合不为空之后存储用户
        }
        if (!CollectionUtils.isEmpty(answers)) {
            questionsAnswersService.savePhones(answers);   //集合不为空之后存储手机
        }
        log.info("存储数据库成功！");
    }
}
